<template>
  <div class="login-container">
    <el-form ref="loginFormRef" :model="loginForm" :rules="rules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <img src="@/assets/common/login-logo.png" alt="">
      </div>

      <el-form-item prop="mobile">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="mobile"
          v-model="loginForm.mobile"
          placeholder="Username"
          name="mobile"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>

      <!-- <el-button @click="hGetUser">登录</el-button> -->

      <div class="tips">
        <span style="margin-right:20px;">用户名: admin</span>
        <span> 密码: 111111</span>
      </div>

    </el-form>
  </div>
</template>

<script>
import { valiadeMobile } from '@/utils/validate'
// import { login } from '@/api/user'
export default {
  name: 'Login',
  data() {
    // 自定义规则函数
    const validateMobile = (rule, value, callback) => {
      if (!valiadeMobile(value)) {
        callback(new Error('手机号输入错误'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        mobile: '13800000002',
        password: '123456'
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          // 手机号需要额外校验，在数去焦点的时候校验自定义的规则函数
          { validator: validateMobile, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在6-10个字符', trigger: 'blur' }
        ]
      }
      // passwordType: 'password',
      // loading: false

    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      // 组件内  this.$refs拿到的是虚拟DOM元素，需要.$el才能拿到真实的DOM元素
      // console.log(this.$refs.loginFormRef)
      // console.log(this.$refs.loginFormRef.$el)
      const that = this
      this.$refs.loginFormRef.validate(function(value) {
        // 表单校验通过则发请求
        if (value) {
          // console.log('发送请求')
          // login(this.loginForm)
          that.hLogin()
        }
      })
    },
    // 抽离发请求函数，使用try catch捕获错误
    async hLogin() {
      try {
        // const res = await login(this.loginForm)
        // console.log(res.data)
        // this.$store.commit('user/setToken', res.data)
        await this.$store.dispatch('user/loginAction', this.loginForm)
        // 跳转至首页
        this.$router.push(this.$route.query.return_url || '/')
      } catch (err) {
        console.log('登录错误，原因是', err)
      }
    }
    // async hGetUser() {
    //   try {
    //     const res = getUser()
    //     console.log('登录成功', res)
    //   } catch (err) {
    //     console.log(err)
    //   }
    // }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#05f1ca;
$cursor: rgb(13, 7, 7);

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-form-item__error{
      color:pink
    }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
  .el-button {
    font-size: 20px;
    height: 65px;
    color: white;
    background-color: #407ffe;
  }

  .el-form-item {
    border: 1px solid #ffffb3;
    background: #fcfcde;
    border-radius: 5px;
    color: #ffffb3;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  // 在style中引入文件的时候，需要在路径里面加~  不然webpack识别不到
  background:url('~@/assets/common/login.jpg') center;
  // 在scope里面修改其他组件的 内部样式的时候，需要使用样式穿透 ::v-deep
  // ::v-deep .el-form-item__error{
  //     color:pink
  //   }
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    img{
      display: block;
      width: 70%;
      margin: 0 auto 30px;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
